﻿using UnityEngine;
using System.Collections;
using System.Net.NetworkInformation;
using System.Net;
using System.IO;
//using SimpleJSON;

public class Loading : MonoBehaviour {

	string MacAddrStr=string.Empty;

	private AsyncOperation mAsync;
	void Awake(){
		gameObject.transform.localScale = new Vector3(1.125f * Screen.width/Screen.height,1,1);		
	}
	
	void Start(){
		//StartCoroutine(CheckDevice());

		MacAddrStr= MacAddr();

	}

	
	// Update is called once per frame
	void Update() {

		if(mAsync!=null && !mAsync.isDone){  
			print("progress:"+(float)mAsync.progress*100+"%");
		}else{
			//Application.LoadLevel("mainScene");
		}
	}
//
//	//start*** check device
//	IEnumerator CheckDevice()
//	{
//		string Kname = null;
//		System.IO.FileStream t_s = null;
//		System.IO.TextReader t_tr = null;
//		try
//		{
//			t_s = System.IO.File.Open(Application.streamingAssetsPath+"/kname",System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite);
//			t_tr = (new System.IO.StreamReader(t_s, System.Text.ASCIIEncoding.ASCII)) as System.IO.TextReader;
//			Kname = MyBase64.Base64ToString(t_tr.ReadLine());
//			t_tr.Close();
//		}catch{}
//		finally
//		{
//			if(t_tr != null)
//			{
//				t_tr.Close();
//				t_tr = null;
//			}
//			if(t_s != null)
//			{
//				t_s.Close();
//				t_s = null;
//			}
//		}
//		Debug.Log("Kname="+Kname);
//		KioskNameInstance.KioskName = Kname;
//		if(Kname == null)
//		{
//			//Application.Quit();
//		}
//		
//		//*** http
//		bool check_ret = false;
//		string kid = Kname;
//		string resStr = string.Empty;
//		bool isChecked = false;
//		try
//		{
//			HttpWebRequest http = WebRequest.Create(link) as HttpWebRequest;
//			http.Method = WebRequestMethods.Http.Post;
//			http.Timeout = 5000;
//			http.ContentType = "application/x-www-form-urlencoded";
//			
//			string tt_mac = MacAddr();
//			Debug.Log("mac="+tt_mac);
//			string data2postAll = arg1 + "=" + kid + "&" + arg2 + "=" + tt_mac;
//			byte[] data = System.Text.Encoding.ASCII.GetBytes(data2postAll);
//			
//			http.BeginGetRequestStream(delegate(System.IAsyncResult ar)
//			                           {
//				try
//				{
//					var hs = http.EndGetRequestStream(ar);
//					hs.Write(data, 0, data.Length);
//					hs.Close();
//					var resp = http.GetResponse();
//					var rs = resp.GetResponseStream();
//					TextReader r = new StreamReader(rs);
//					resStr = r.ReadToEnd();
//					r.Close();
//					rs.Close();
//					isChecked = true;
//				}
//				catch
//				{
//					isChecked = true;
//				}
//			}, http);
//		}
//		catch
//		{
//			isChecked = true;
//		}
//		while (!isChecked)
//		{
//			yield return new UnityEngine.WaitForEndOfFrame();
//		}
//		try 
//		{
//			Debug.Log(resStr);
//			var json = JSON.Parse(resStr);
//			if (json[resultObj].Value == resultSUCCESS)
//			{
//				check_ret = true;
//			}
//			else
//			{
//				check_ret = false;
//			}
//		}
//		catch
//		{
//			check_ret = false;
//		}
//		if(!check_ret)
//		{
//			Application.Quit();
//		}
//		//***
//		if(check_ret)
//		{
//			StartCoroutine("Load");
//		}
//	}
	
	string arg1 = "deviceId";
	string arg2 = "mId";
	string resultSUCCESS = "SUCCESS";
	string resultFAILURE = "FAILURE";
	string resultObj = "result";
	string link = "http://120.26.77.61:8080/sideshop/deviceAction!init";
	
	string MacAddr()
	{
		if (NetworkInterface.GetIsNetworkAvailable())
		{
			var ms = NetworkInterface.GetAllNetworkInterfaces();
			for (int i = 0; i < ms.Length; ++i)
			{
				{
					if (ms[i].OperationalStatus == OperationalStatus.Up && (!ms[i].IsReceiveOnly) && ms[i].NetworkInterfaceType != NetworkInterfaceType.Tunnel && ms[i].NetworkInterfaceType != NetworkInterfaceType.Loopback)
					{
						return ms[i].GetPhysicalAddress().ToString();
					}
				}
			}
		}
		return null;
	}
	//end***





	void OnGUI()
	{
		GUILayout.Label("MacAddrStr:  "+MacAddrStr    );


	}









}

///http://www.jb51.net/softjc/233304.html   xia zhai zhe ge li zi
